// pages/distribution/index.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        style: false,
        way: 0,
        poster: false
    },
    onLoad: function(options) {
        wx.showLoading({
            title: '加载中...',
        })
    },
    onShow() {
        this.loadData()
    },
    loadData() {
        Promise.all([
            app.http.get('account/statistics-count', {
                data: {
                    type: 2
                }
            }),
            app.http.get('account/statistics', {
                data: {
                    type: 2
                }
            }),
            app.http.post('user/my'),
            app.http.get('account/config'),
        ]).then(res => {
            // res[1].data.txCondition = parseFloat(res[1].data.profit_widthdraw) >= parseFloat(res[3].data.stroke_amount_withdraw_floor_limit) //提现下限
            res[1].data.txCondition = true
            this.setData({
                numTotal: res[0].data,
                commission: res[1].data,
                user: res[2].data.user,
                config: res[3].data
            })
            wx.hideLoading()
        })
    },

    // loadTxstyle() {
    //     app.http.get('distribution/cash-style').then(res => {
    //         let list = res.data.list
    //         list.forEach(function(item, index, input) {
    //             input[index] = item == 1 ? '商城余额' : item == 2 ? '微信钱包' : item == 3 ? '手动提现到支付宝' : item == 4 ? '手动提现到银行卡' : ''
    //         })
    //         this.setData({
    //             list: list
    //         })
    //     }).catch(err => {

    //     })
    // },
    radioChange(e) {
        this.setData({
            way: e.detail.value
        })
    },
    cashTap() {
        wx.navigateTo({
            url: '/pages/wallet/tx?type=2',
        })
        // this.setData({
        //     style: true
        // })
    },
    closeTap() {
        this.setData({
            style: false
        })
    },
    // txSubmit() {
    //     console.log(this.data.way)
    //     if (this.data.way == 0) { //商城余额
    //         wx.navigateTo({
    //             url: '',
    //         })
    //     } else if (this.data.way == 1) { //微信钱包
    //         wx.navigateTo({
    //             url: '',
    //         })
    //     } else if (this.data.way == 2) { //支付宝
    //         wx.navigateTo({
    //             url: '/pages/personal/wallet/tx?type=3',
    //         })
    //     } else if (this.data.way == 3) { //银行卡
    //         wx.navigateTo({
    //             url: '/pages/personal/wallet/tx?type=4',
    //         })
    //     }
    // },
    // 生成海报
    isPoster() {
        this.setData({
            poster: !this.data.poster
        })
    },
    saveImageToPhotosAlbum: function() {
        wx.showLoading({
            title: '生成海报中...'
        })
        var that = this;
        //设置画板显示，才能开始绘图
        that.setData({
            canvasHidden: false,
        })

        function download(url) {
            return new Promise(resolve => {
                wx.downloadFile({
                    url: url,
                    success: res => {
                        resolve(res.tempFilePath);
                    }
                })
            })
        }
        var unit = app.scrollWidth / 375
        var ctx = wx.createCanvasContext('poster')
        ctx.setFillStyle('#fff')
        ctx.font = "10px PingFang SC"
        ctx.fillRect(0, 0, unit * 300, unit * 430)

        download('https://img.zhxcxmall.com/ll/distribution-poster-bg.jpg').then(r => {
            ctx.drawImage(r, 0, 0, unit * 300, unit * 430);
            return download(that.data.inviteQrCode)
        }).then(r => {
            ctx.drawImage(r, unit * 115, unit * 339, unit * 70, unit * 70);
            ctx.setTextAlign('center')
            ctx.setFontSize(11)
            ctx.setFillStyle("#333")
            ctx.fillText('扫一扫变身达人', unit * 150, unit * 424)
        }).then(r => {
            ctx.draw(false, function() {
                that.setData({
                    poster: true,
                })
                wx.hideLoading()
            })

        })
    },
    drawText: function(ctx, str, initHeight, canvasWidth, unit) {
        var lineWidth = 0;
        var lastSubStrIndex = 0; //每次开始截取的字符串的索引
        for (let i = 0; i < str.length; i++) {
            lineWidth += ctx.measureText(str[i]).width;
            if (lineWidth > canvasWidth) {
                ctx.fillText(str.substring(lastSubStrIndex, i), unit * 125, initHeight); //绘制截取部分
                initHeight += unit * 15; //两行之间字体的高度
                lineWidth = 0;
                lastSubStrIndex = i;
            }
            if (i == str.length - 1) { //绘制剩余部分
                ctx.fillText(str.substring(lastSubStrIndex, i + 1), unit * 125, initHeight);
            }
        }
    },
    savePoster() {
        var that = this
        var unit = app.scrollWidth / 375
        wx.showLoading({
            title: '保存中',
        })
        wx.canvasToTempFilePath({
            x: 0,
            y: 0,
            width: unit * 300,
            height: unit * 430,
            destWidth: unit * 1800,
            destHeight: unit * 2580,
            quality: 1,
            canvasId: 'poster',
            success: res => {
                that.setData({
                    scope: false
                })
                wx.saveImageToPhotosAlbum({
                    filePath: res.tempFilePath,
                    success: (res) => {
                        wx.showModal({
                            title: '温馨提示',
                            content: '分销海报已保存到手机相册',
                            showCancel: false,
                            confirmColor: '#ff5400'
                        })
                        wx.hideLoading()
                        that.setData({
                            canvasHidden: true,
                            poster: false,
                        })
                    },
                    fail: (err) => {
                        console.log(err, 'xxxxxxxxxx');
                        wx.hideLoading()
                        wx.showToast({
                            title: '保存失败，请稍后再试',
                            icon: 'none'
                        })
                        if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
                            that.setData({
                                scope: true
                            })
                        }
                    }
                })
            }
        })
    }
})